let arr = read_line().split(" ");

let n = parseInt(arr[0]);
let m = parseInt(arr[1]);

let arr1 = read_line()
  .split(" ")
  .map((item) => parseInt(item));

let arr2 = read_line()
  .split(" ")
  .map((item) => parseInt(item));

let l = 0;
let r = m;

let mid = 0;

arr2.sort((a, b) => a - b);

while (mid < m) {
  let sign = true;

  let data = [];

  for (let i = 0; i < arr1.length; i++) {
    data[i] = (arr1[i] + mid) % m;
  }

  data.sort((a, b) => a - b);

  for (let i = 0; i < data.length; i++) {
    if (data[i] !== arr2[i]) {
      sign = false;
      break;
    }
  }

  if (sign) {
    break;
  } else {
    mid++;
  }
}

print(mid);
